/*
Field.cpp
Field implemnatation file
Zach Agudelo, Andi Cescolini, Sam Tarin

Field is the main class for the program that runs the its main functions
in OnLoop to cycle through gameplay, OnRender to update, and onInit/onCleanup
to open/close */

#include "Field.h"
#include <string>
#include <cmath>
//UNCOMMENT THIS FOR LINUX ONLY
#include "FieldEntity.cpp"
#include "FieldAnimation.cpp"

#include <iostream>

using namespace std;


Field::Field() :FieldEvent() {   //constructor
    //set starting values
    Surf_Field = NULL;
    Surf_Player = NULL;
    Surf_Display = NULL;
    Running = true;
    up=down=left=right=handoffkey=tdgo=losergo=hikeball=0;
    who=16;
    canPass=1;
    click=0;
}



int Field::OnExecute() {
    //if it can't start
    if(OnInit() == false) {
        return -1;
    }
 
    SDL_Event Event;//event for key press or mouse click
 
    //main loop
    while(Running) {
        while(SDL_PollEvent(&Event)) {//handles events
            OnEvent(&Event);
        }
        OnLoop(); //updates positions and movement
        OnRender(); //draws things to screen
    }
    OnCleanup();//clears memory uesd
    return 0;
}

//quits everything when called
void Field::OnCleanup() {
    //free entitiylist
    for(int i = 0;i < FieldEntity::EntityList.size();i++) {
        if(!FieldEntity::EntityList[i]) continue;
 
        FieldEntity::EntityList[i]->OnCleanup();
    }
    FieldEntity::EntityList.clear();
   
    //free surfaces
    SDL_FreeSurface(Surf_Field);
    SDL_FreeSurface(Surf_Display);
    SDL_FreeSurface(Surf_Player);
    SDL_FreeSurface(Surf_TD);
    SDL_Quit();
}

//acts on an event
void Field::OnEvent(SDL_Event* Event) {
    FieldEvent::OnEvent(Event);//send to the base class FieldEvent to pick proper event there
}
//for the mouse click
void Field::OnLButtonDown(int mX, int mY){
     currentmx=(float)mX;
     currentmy=(float)mY;    
     click=1;
     ydifference=(currentmy-newballEntity.Y);  //changed this so it throws the ball, not the qb
     xdifference=(currentmx-newballEntity.X);
     slope=(ydifference)/(xdifference);
     angle=atan(fabs(slope));           //calculates angle to point
}
//Acts when the key is pressed
void Field::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode){
    //swicthes on the proper key pressed
     switch(sym) {
        case SDLK_UP:    up=1; break;
        case SDLK_DOWN:  down=1; break;
        case SDLK_LEFT:  left=1; break;
        case SDLK_RIGHT: right=1; break;
        case SDLK_RETURN: handoffkey=1; break;
        case SDLK_SPACE:
              if(live==0 && (tdgo==1 || losergo==1)){
                  afterTD();
                  tdgo=0;
                  losergo = 0;
              }
              else{
                  hikeball=1;
              }
              break;
 
        default: {
        }
    }
   
}
//acts on key released
void Field::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
    //switch on which key was pressed
    switch(sym) {
        case SDLK_UP:    up=0; break;
        case SDLK_DOWN:  down=0; break;
        case SDLK_LEFT:  left=0; break;
        case SDLK_RIGHT: right=0; break;
        case SDLK_RETURN: handoffkey=0; break;
 
        default: {
        }
    }
}

//initializes the bitmaps and the display (loads the data)
bool Field::OnInit() {
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {//initialize all
        return false;
    }
    //main display initialize
    if((Surf_Display = SDL_SetVideoMode(640, 680, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {
        return false;
    }
    //loads bmps
    char fieldpic[10]="field.bmp";
    char playerpic[15]= "playerbmps.bmp";
    char ballpic[13]= "ballbmps.bmp";
    char tdpic[14]= "touchdown.bmp";
    char loserpic[10]= "loser.bmp";

    if((Surf_Field = FieldSurface::OnLoad(fieldpic)) == NULL) {
        return false;
    }
        if((Surf_Player = FieldSurface::OnLoad(playerpic)) == NULL) {
        return false;
    }
   
    if((Surf_Ball = FieldSurface::OnLoad(ballpic)) == NULL) {
        return false;
    }
    if((Surf_TD = FieldSurface::OnLoad(tdpic)) == NULL) {
        return false;
    }
    if((Surf_TD = FieldSurface::OnLoad(loserpic)) == NULL) {
        return false;
    }
 
    FieldSurface::Transparent(Surf_Player, 255, 255, 255);





    //loads player and ball entity
   
   
    //file, width, height, frames, xpos, ypos, animation, status
    //BALL AND FIELD AND TOUCHDOWN
    if(newballEntity.OnLoad(ballpic, 38, 38, 1, 300, 400, 0, 1)== false){//loading into the ball
    return false;  
    }
    if(footballFieldEntity.OnLoad(fieldpic, 640, 680, 0, 0, 0, 0, 0) == false) { //loads the field
        return false;
    }
    if(touchdown.OnLoad(tdpic, 312, 312, 0, 164, 174 , 0, 0) == false) {
       return false;
    }
    if(loser.OnLoad(loserpic, 312, 312, 0, 164, 174, 0, 0) == false){
       return false;
    }
   
    //OFFENSE

    if(qbEntity.OnLoad(playerpic, 38, 38, 7,  300, 500, 0, 1) == false) {
        return false;
    }
    if(player1Entity.OnLoad(playerpic, 38, 38, 7, 240, 400, 0, 1) == false) {
        return false;
    }
    if(player2Entity.OnLoad(playerpic, 38, 38, 7, 280, 400, 0, 1) == false) {
        return false;
    }
    if(player3Entity.OnLoad(playerpic, 38, 38, 7, 320, 400, 0, 1) == false) {
        return false;
    }
    if(player4Entity.OnLoad(playerpic, 38, 38, 7, 360, 400, 0, 1) == false) {
        return false;
    }
   
    if(player9Entity.OnLoad(playerpic, 38, 38, 7, 140, 400, 0, 4) == false)   {   //first wide receiver
        return false;
    }
    if(player10Entity.OnLoad(playerpic, 38, 38, 7, 460, 400, 0, 5) == false) {   //second wide receiver
        return false;
    }
    if(player14Entity.OnLoad(playerpic, 38, 38, 7, 390, 400, 0, 6) == false) {   //running back
        return false;
}
    //DEFENSE
    if(player5Entity.OnLoad(playerpic, 38, 38, 7, 240, 335, 1, 2) == false) {
        return false;
    }
    if(player6Entity.OnLoad(playerpic, 38, 38, 7, 280, 335, 1, 2) == false) {
        return false;
    }
    if(player7Entity.OnLoad(playerpic, 38, 38, 7, 320, 335, 1, 2) == false) {
        return false;
    }
    if(player8Entity.OnLoad(playerpic, 38, 38, 7, 360, 335, 1, 2) == false) {
        return false;
    }

    if(player11Entity.OnLoad(playerpic, 38, 38, 7, 200, 235, 1, 7) == false) { //follows first wide receiver
        return false;
    }
    if(player12Entity.OnLoad(playerpic, 38, 38, 7, 400, 235, 1, 8) == false) { //follows second wide receiver
        return false;
    }
    if(player13Entity.OnLoad(playerpic, 38, 38, 7, 300, 235, 1, 9) == false) {  // safety
        return false;
    }
   
    if(player15Entity.OnLoad(playerpic, 38, 38, 7, 200, 335, 1, 2) == false) {
        return false;
    }
   
   
    if(transitionEntity.OnLoad(playerpic, 38, 38, 7, 200, 150, 1, 0) == false) {   //USED TO CHANGE CONTROL
        return false;
    }
    //puts entity pointers in list POLYMORPHISM! -> Do not put in transitionEntity
    FieldEntity::EntityList.push_back(&footballFieldEntity); //0
    FieldEntity::EntityList.push_back(&player14Entity); //1
    FieldEntity::EntityList.push_back(&player15Entity); //2
    FieldEntity::EntityList.push_back(&player13Entity); //3
    FieldEntity::EntityList.push_back(&player12Entity); //4
    FieldEntity::EntityList.push_back(&player11Entity); //5
    FieldEntity::EntityList.push_back(&player10Entity); //6
    FieldEntity::EntityList.push_back(&player9Entity); //7
    FieldEntity::EntityList.push_back(&player8Entity); //8
    FieldEntity::EntityList.push_back(&player7Entity); //9
    FieldEntity::EntityList.push_back(&player6Entity); //10
    FieldEntity::EntityList.push_back(&player5Entity); //11
    FieldEntity::EntityList.push_back(&player4Entity); //12
    FieldEntity::EntityList.push_back(&player3Entity); //13
    FieldEntity::EntityList.push_back(&player2Entity); //14
    FieldEntity::EntityList.push_back(&player1Entity); //15
    FieldEntity::EntityList.push_back(&qbEntity); //16
    FieldEntity::EntityList.push_back(&newballEntity); //17
         
    downNumber=1;
    live=0;
   
    return true;
}

void Field::OnExit(){  //allows user to close program by clicking "X" in top corner
     Running=false;
}

void Field::OnLoop() {


    if(live)
    {
        //CHANGES TO COORDINATES MAKE ANIMATION
        for(int i = 0;i < FieldEntity::EntityList.size();i++) {
        if(!FieldEntity::EntityList[i]) continue;
     
        FieldEntity::EntityList[i]->OnLoop();
        }
       
       
       
 
        if(click== 0){
           
          //moveBall function (x change,y change), animstate changes direction players are running
        if(up){ moveBall(0,-4, who);  FieldEntity::EntityList[who]->AnimState = 3;}
        if(right){ moveBall(4,0, who); FieldEntity::EntityList[who]->AnimState = 9;}
        if(left){ moveBall(-4,0, who); FieldEntity::EntityList[who]->AnimState = 10;}
        if(down){ moveBall(0,4, who); FieldEntity::EntityList[who]->AnimState = 0;}
        }
        if(handoffkey) handOff();
     
     
       
 
        //Defenders follow the football ///////////////////////////////////////////
        float checkDefY =0;
        float checkDefX = 0;
        float checkDefStatus = 0;
       
       
                 //loop through all players
          for(int i = 1;i < FieldEntity::EntityList.size();i++) {
            checkDefY = FieldEntity::EntityList[i]->Y;
            checkDefX = FieldEntity::EntityList[i]->X;
            checkDefStatus = FieldEntity::EntityList[i]->status;
                                  //if they are defenders following the qb
              if (checkDefStatus ==2){
                if(checkDefY < qbEntity.Y ){FieldEntity::EntityList[i]->Y+=.5;}
                if(checkDefY > qbEntity.Y ){FieldEntity::EntityList[i]->Y-=.5;}
                if(checkDefX < qbEntity.X ){FieldEntity::EntityList[i]->X+=.5;}
                if(checkDefX > qbEntity.X ){FieldEntity::EntityList[i]->X-=.5;}
              }
                                  //if they are defenders following player1
              if(checkDefStatus == 3){
                if(checkDefY < player1Entity.Y ){FieldEntity::EntityList[i]->Y+=.5;}
                if(checkDefY > player1Entity.Y ){FieldEntity::EntityList[i]->Y-=.5;}
                if(checkDefX < player1Entity.X ){FieldEntity::EntityList[i]->X+=.5;}
                if(checkDefX > player1Entity.X ){FieldEntity::EntityList[i]->X-=.5;}
                               
              }
              if((checkDefStatus == 7 || checkDefStatus == 8 || checkDefStatus == 9) && canPass == 0 ){ //follows the first WR
                 FieldEntity::EntityList[i]->status= 2;
              }
              if(checkDefStatus == 7 && canPass == 1) { //if the quarterback hasn't thrown the ball yet or crossed the LOS
                 if(checkDefY < player9Entity.Y ){FieldEntity::EntityList[i]->Y+=.4;}
                 if(checkDefY > player9Entity.Y ){FieldEntity::EntityList[i]->Y-=.4;}
                 if(checkDefX < player9Entity.X ){FieldEntity::EntityList[i]->X+=.4;}
                 if(checkDefX > player9Entity.X ){FieldEntity::EntityList[i]->X-=.4;}
              }  

              if(checkDefStatus == 8 && canPass == 1) { //if the quarterback hasn't thrown the ball yet or crossed the LOS
                 if(checkDefY < player10Entity.Y ){FieldEntity::EntityList[i]->Y+=.55;}
                 if(checkDefY > player10Entity.Y ){FieldEntity::EntityList[i]->Y-=.55;}
                 if(checkDefX < player10Entity.X ){FieldEntity::EntityList[i]->X+=.55;}
                 if(checkDefX > player10Entity.X ){FieldEntity::EntityList[i]->X-=.55;}
              }  
            }
         
//OFFENSE RUN ROUTES
            float checkY = 0;
            float checkX = 0;
            float checkStatus = 0;
        float checkXint = 0;
        float checkYint = 0;

        //OFFENSIVE LINE
          for(int i = 1;i < FieldEntity::EntityList.size();i++) {
                                //store in floats for comparison
            checkY = FieldEntity::EntityList[i]->Y;
            checkX = FieldEntity::EntityList[i]->X;
            checkStatus = FieldEntity::EntityList[i]->status;
                                  //if they are defenders following the qb
              if (checkStatus == 1 && i != 16 && i != 17){
              //  if(qbEntity.Y < qbEntity.yInitial ){
                  if((fabs(newballEntity.Y-checkY) < 120)&&newballEntity.Y-50 < checkY){FieldEntity::EntityList[i]->Y-=.3;}
                  if((fabs(newballEntity.Y-checkY) < 120)&&newballEntity.Y-50 > checkY){FieldEntity::EntityList[i]->Y+=.3;}
                  if(newballEntity.Y < checkY){FieldEntity::EntityList[i]->Y-=.7;}
              //  }                  
               // if(qbEntity.Y > qbEntity.yInitial ){FieldEntity::EntityList[i]->Y+= .3;}
                if(checkX < newballEntity.X ){FieldEntity::EntityList[i]->X+=.3;}
                if(checkX > newballEntity.X ){FieldEntity::EntityList[i]->X-=.3;}
              }
          }


            //First wide receiver

          for(int i = 1;i < FieldEntity::EntityList.size();i++) {
            checkY = FieldEntity::EntityList[i]->Y;
            checkX = FieldEntity::EntityList[i]->X;
        checkXint = FieldEntity::EntityList[i]->xInitial;
        checkYint = FieldEntity::EntityList[i]->yInitial;
            checkStatus = FieldEntity::EntityList[i]->status;
            if (checkStatus == 4){
               if(checkX <= 100 && checkY>100){FieldEntity::EntityList[i]->Y-=.5; FieldEntity::EntityList[i]->AnimState = 3;}
               if(checkX > 100 ){FieldEntity::EntityList[i]->X-=.5; FieldEntity::EntityList[i]->AnimState = 10;}
            }
          }
          //Second Wide Receiver
          for(int i = 1;i < FieldEntity::EntityList.size();i++) {
            checkY = FieldEntity::EntityList[i]->Y;
            checkX = FieldEntity::EntityList[i]->X;
        checkXint = FieldEntity::EntityList[i]->xInitial;
        checkYint = FieldEntity::EntityList[i]->yInitial;
            checkStatus = FieldEntity::EntityList[i]->status;
            if (checkStatus == 5){
               if(checkX <= 500 ){FieldEntity::EntityList[i]->X+=.75; FieldEntity::EntityList[i]->AnimState = 9;}
               if(checkX > 500  && checkY>100){FieldEntity::EntityList[i]->Y-=1; FieldEntity::EntityList[i]->AnimState = 3;}
            }
          }      
          //Runnning Back
             for(int i = 1;i < FieldEntity::EntityList.size();i++) {
            checkY = FieldEntity::EntityList[i]->Y;
            checkX = FieldEntity::EntityList[i]->X;
            checkStatus = FieldEntity::EntityList[i]->status;
        checkXint = FieldEntity::EntityList[i]->xInitial;
        checkYint = FieldEntity::EntityList[i]->yInitial;
            if (checkStatus == 6){
               if(checkY <= checkYint+50 && checkX > checkXint-40){FieldEntity::EntityList[i]->Y+=3; FieldEntity::EntityList[i]->AnimState = 0;}
               if(checkY > checkYint+50 && checkX > checkXint-200){FieldEntity::EntityList[i]->X-=2; FieldEntity::EntityList[i]->AnimState = 10;}
               if(checkX <= checkXint-200 && checkY> checkYint-100){FieldEntity::EntityList[i]->Y-=1; FieldEntity::EntityList[i]->AnimState = 3;}
            }
          }


    //Handle tackles
        for(int i = 1;i < FieldEntity::EntityList.size()-1;i++) {
            checkDefY = FieldEntity::EntityList[i]->Y;
            checkDefX = FieldEntity::EntityList[i]->X;
            checkDefStatus = FieldEntity::EntityList[i]->status;
              if (checkDefStatus ==2 || checkDefStatus==7 || checkDefStatus==8 || checkDefStatus ==9){
                if(fabs(checkDefY-qbEntity.Y)<38 && fabs(checkDefX-qbEntity.X)<38){
           if(click==0){
               OnTackle();
                       live=0;
               }
                }
            }
        }
 
   
         //Don't let characters overlap //////////////////////////////////////
       
   
        float upperFirstX = 0;
        float upperFirstY = 0;
        float upperSecondX = 0;
        float upperSecondY = 0;
       
        float widthFirst = 0;
        float heightFirst = 0;
        float widthSecond = 0;
        float heightSecond = 0;
       
        float lowerFirstX = 0;
        float lowerFirstY = 0;
        float lowerSecondX = 0;
        float lowerSecondY = 0;
       
        int noDoubleCollission = 0;
       
    //      if(down||up||left||right)
          {
          for(int i = 1;i < FieldEntity::EntityList.size()-1;i++) {
            for(int j = 1;j < FieldEntity::EntityList.size()-1;j++) {
              widthFirst = FieldEntity::EntityList[i]->Width;
              heightFirst = FieldEntity::EntityList[i]->Height;
              widthSecond = FieldEntity::EntityList[j]->Width;
              heightSecond = FieldEntity::EntityList[j]->Height;
              //using upper left and lower right
              upperFirstX = FieldEntity::EntityList[i]->X;
              upperFirstY = FieldEntity::EntityList[i]->Y;
              lowerFirstX = upperFirstX-widthFirst;
              lowerFirstY = upperFirstY-heightFirst;
              upperSecondX = FieldEntity::EntityList[j]->X;
              upperSecondY = FieldEntity::EntityList[j]->Y;
              lowerSecondX = upperSecondX-widthSecond;
              lowerSecondY = upperSecondY-heightSecond;
              if(noDoubleCollission ==0){
                if((upperFirstX < upperSecondX && upperFirstX > lowerSecondX) && (upperFirstY <= upperSecondY && upperFirstY >= lowerSecondY)){
                  FieldEntity::EntityList[j]->X+=5;    
                  FieldEntity::EntityList[i]->X-=1;
                  noDoubleCollission =1;                          
                }
                if((upperFirstX < upperSecondX && upperFirstX > lowerSecondX) && (upperFirstY > upperSecondY && upperSecondY > lowerFirstY)){
                  FieldEntity::EntityList[i]->Y+=5;
                  FieldEntity::EntityList[j]->Y-=1;
                  noDoubleCollission =1;                                                
                }
             
              }
              //using upper right and lower left
              lowerFirstX = FieldEntity::EntityList[i]->X;
              upperFirstX = lowerFirstX+widthFirst;
              upperFirstY = FieldEntity::EntityList[i]->Y;
              lowerFirstY = upperFirstY-heightFirst;
              lowerSecondX = FieldEntity::EntityList[j]->X;
              upperSecondX = lowerSecondX+widthSecond;
              upperSecondY = FieldEntity::EntityList[j]->Y;
              lowerSecondY = upperSecondY-heightSecond;
              if(noDoubleCollission ==0){
                if((upperFirstX > upperSecondX && upperSecondX > lowerFirstX) && (upperFirstY <= upperSecondY && upperFirstY >= lowerSecondY)){
                  FieldEntity::EntityList[j]->X+=1;    
                  FieldEntity::EntityList[i]->X-=1;
                  noDoubleCollission =1;                                  
                }
                if((lowerFirstX > lowerSecondX && lowerFirstX < lowerSecondX) && (lowerFirstY > lowerSecondY && lowerFirstY < upperSecondY)){
                  FieldEntity::EntityList[j]->X+=1;    
                  FieldEntity::EntityList[i]->X-=1;
                  noDoubleCollission =1;                                  
                }
              }        
            }
            noDoubleCollission =0;
          }
        }  

   if(qbEntity.Y < newballEntity.yInitial)
   {
    canPass=0;
    //cout<< "SET CAN PASS TO ZERO" << endl;
   }

   if(!canPass) click=0;
   
    //PASSING THE BALL
        //WHEN YDIFFERENCE IS LESS THAN 0, BALL IS GOING "UP" THE SCREEN
        if(click==1){
           if((xdifference> 0) && (ydifference>0)){  //going down and to the right
               if(fabs(newballEntity.X-currentmx)<1 || fabs(newballEntity.Y-currentmy)<1 || newballEntity.X>currentmx || newballEntity.Y>currentmy){ //ADDING TO BALL CAN BE GREATER THAN MOUSE
                  click=0;
                  checkCatch();
                }
               else{
                  ///////////////////////////move ball test//////////////////////////////////////  
                  //moveBall(2*cos(angle),2*sin(angle));  
                   
                  newballEntity.X+=2*cos(angle);
                  newballEntity.xInitial+=2*cos(angle);
                  //intSlope = slope;
                  newballEntity.Y+= 2*sin(angle);
                  newballEntity.yInitial+= 2*sin(angle);
   
                }
           }
              else if((xdifference > 0) && (ydifference <= 0)) { //going up and to the right
                 if(fabs(newballEntity.X-currentmx)<1 || fabs(newballEntity.Y-currentmy)<1 || newballEntity.X>currentmx || newballEntity.Y<currentmy){ //ADDING TO BALL CAN BE GREATER THAN MOUSE
                    click=0;
                    checkCatch();
                 }
                 else{
                    //moveBall(2*cos(angle),-2*sin(angle));  
                    newballEntity.X+=2*cos(angle);  //depends on which way the ball is traveling down the field
                    newballEntity.xInitial+=2*cos(angle);
                    //intSlope = slope;
                    newballEntity.Y-= 2*sin(angle);
                    newballEntity.yInitial-= 2*sin(angle);          
                 }
           }
               else if((xdifference <= 0) && (ydifference <= 0)) { //going up and to the left
                 if(fabs(newballEntity.X-currentmx)<1 || fabs(newballEntity.Y-currentmy || newballEntity.X<currentmx || newballEntity.Y<currentmy)<1){
                    click=0;
                    checkCatch();
                 }
                 else{
                    //moveBall(-2*cos(angle),-2*sin(angle));  
                    newballEntity.X-=2*cos(angle);  
                    newballEntity.xInitial-=2*cos(angle);
                    //intSlope = slope;
                    newballEntity.Y-= 2*sin(angle);
                    newballEntity.yInitial-= 2*sin(angle);
                 }
           }
               else if((xdifference <= 0) && ( ydifference > 0)) { //down and to the left
                 if(fabs(newballEntity.X-currentmx)<1 || fabs(newballEntity.Y-currentmy || newballEntity.X<currentmx || newballEntity.Y>currentmy)<1){
                    click=0;
              checkCatch();
                 }
                 else{
                    //moveBall(-2*cos(angle),2*sin(angle));
                    newballEntity.X-=2*cos(angle);  
                    newballEntity.xInitial-=2*cos(angle);
                    //intSlope = slope;
                    newballEntity.Y+= 2*sin(angle);
                    newballEntity.yInitial+= 2*sin(angle);
                 }
           }
           
        }
       

    }//close if(live)

    if(hikeball){
       hike();
    }    

        //makes sure the player and the ball stay together if one of the entities gets hit
    if(((FieldEntity::EntityList[who]->X != newballEntity.X) || (FieldEntity::EntityList[who]->Y != newballEntity.Y)) && !click && live){
       newballEntity.X= FieldEntity::EntityList[who]->X;
       newballEntity.Y= FieldEntity::EntityList[who]->Y;
    }
  //  cout<< "can pass = " << canPass << endl;
   // cout << "click = " << click << endl;

} //closes onLoop






//displays the field and players
void Field::OnRender() {
     //displays the field
     //X(onmap), Y(onmap), X(initialBMP),Y(initialBMP), X(finalBMP), Y(finalBMP)
    FieldSurface::OnDraw(Surf_Display, Surf_Field, 0, 0, 0, 0, 640, 600);
   
    // temporary call for displaying the players
    for(int i = 0;i < FieldEntity::EntityList.size();i++) {
        if(!FieldEntity::EntityList[i]) continue;
 
        FieldEntity::EntityList[i]->OnRender(Surf_Display);
    }
    if(tdgo == 1) {
       touchdown.OnRender(Surf_Display);
    }
    if(losergo == 1) {
               loser.OnRender(Surf_Display);
    }
    SDL_Flip(Surf_Display);
}

void Field::OnTackle()
{
    float Ychange;
   
    if(qbEntity.Y > 540){
       onLoser();
   }
   else{
   
    Ychange=qbEntity.Y-qbEntity.resetY;
 
    qbEntity.Y+=100;  //sets the qb behind the line
    qbEntity.X=qbEntity.resetX;    //sets him back to the center of the field
    newballEntity.Y=qbEntity.Y-100; //ball starts with the qb
    newballEntity.X=qbEntity.X;    

    qbEntity.AnimState = 0;

 //reset player's at down
    player1Entity.status = 1; player1Entity.AnimState = 0;
    player2Entity.status = 1; player2Entity.AnimState = 0;
    player3Entity.status = 1; player3Entity.AnimState = 0;
    player4Entity.status = 1; player4Entity.AnimState = 0;
    player5Entity.status = 2; player5Entity.AnimState = 1;
    player6Entity.status = 2; player6Entity.AnimState = 1;
    player7Entity.status = 2; player7Entity.AnimState = 1;
    player8Entity.status = 2; player8Entity.AnimState = 1;
    player9Entity.status = 4; player9Entity.AnimState = 0;
    player10Entity.status = 5; player10Entity.AnimState = 0;
    player11Entity.status = 7; player11Entity.AnimState = 1;
    player12Entity.status = 8; player12Entity.AnimState = 1;
    player13Entity.status = 9; player13Entity.AnimState = 1;
    player14Entity.status = 6; player14Entity.AnimState = 0;
   
       if(newballEntity.Y > 130){
      for(int i = 1; i< FieldEntity::EntityList.size()-1;i++){
        (FieldEntity::EntityList[i])->setOriginal(Ychange+100);
        if(FieldEntity::EntityList[i]->Y < 0){
          FieldEntity::EntityList[i]->Y = 30;
          }
      }
    }
    if(newballEntity.Y < 130){
         for(int i = 1; i< FieldEntity::EntityList.size()-1; i++){
           int checkStatus =0;
           checkStatus = FieldEntity::EntityList[i]->status;
           if((checkStatus ==1 || checkStatus == 4 || checkStatus == 5 || checkStatus ==6)){
             FieldEntity::EntityList[i]->Y = newballEntity.Y;
             FieldEntity::EntityList[i]->X = FieldEntity::EntityList[i]->resetX;
           }
           if(checkStatus == 2 || checkStatus == 7 || checkStatus ==8 || checkStatus ==9){
             FieldEntity::EntityList[i]->Y = 90;
             FieldEntity::EntityList[i]->X = FieldEntity::EntityList[i]->resetX;
           }
         }
         qbEntity.Y = qbEntity.Y+100;
    }  



    if(downNumber>3) onLoser();
    downNumber++;
    canPass=1;
}
}

void Field::moveBall(float dx,float dy, int who)
{
    //    //Move the football around! ///////////////////////////////
    //STOPS AT ENDZONES
    if(FieldEntity::EntityList[who]->Y >= 60){            
      if(dy<0){ FieldEntity::EntityList[who]->Y+=dy; newballEntity.Y+=dy; /*ballEntity.yInitial+=dy;*/}//going up
         if(FieldEntity::EntityList[who]->Y <= 85){
            onTouchdown();
         }
               
    }
    if(FieldEntity::EntityList[who]->Y <= 642){
      if(dy>0){ FieldEntity::EntityList[who]->Y+=dy; newballEntity.Y+=dy; /*ballEntity.yInitial+=dy;*/} //going down
    }
     //CHECKS HORIZONTAL BOUNDS, STOPS AT TOUCHLINES
    if(FieldEntity::EntityList[who]->X >=0){
      if(dx<0) {FieldEntity::EntityList[who]->X+=dx; newballEntity.X+=dx; /*ballEntity.xInitial +=dx;*/} //left
    }
    if(FieldEntity::EntityList[who]->X <= 602){
      if(dx>0) {FieldEntity::EntityList[who]->X+=dx; newballEntity.X+=dx; /*ballEntity.xInitial +=dx;*/} //right
    }

}


void Field::handOff()
{
   
  ////////// HAND OFF THE BALL! ////////////////////////////////////
  float checkOffY = 0;
  float checkOffX = 0;
  int checkOffStatus = 0;
  //loops through all the players on the field
  for(int i = 1;i < FieldEntity::EntityList.size();i++) {
    checkOffY = FieldEntity::EntityList[i]->Y;
    checkOffX = FieldEntity::EntityList[i]->X;
    checkOffStatus = FieldEntity::EntityList[i]->status;
	 //finds player on offense
    if (checkOffStatus == 1 || checkOffStatus == 4 || checkOffStatus == 5 || checkOffStatus == 6){
		//finds player nearest to current player and give them the ball
      if(fabs(checkOffY-newballEntity.Y)<50 && fabs(checkOffX-newballEntity.X)<50 ){
        changeControl(i);
        handoffkey=0;                
      }
    }              
  }

}

void Field::changeControl(int i)
{
     //transition entity becomes ball entity                              
       transitionEntity = qbEntity;
     //ball entity becomes all parts of the player switching too  
       qbEntity.X = FieldEntity::EntityList[i]->X-10;
       qbEntity.Y = FieldEntity::EntityList[i]->Y;
       qbEntity.Width = FieldEntity::EntityList[i]->Width;
       qbEntity.Height = FieldEntity::EntityList[i]->Height;
       qbEntity.AnimState = FieldEntity::EntityList[i]->AnimState;
       qbEntity.MaxFrames = FieldEntity::EntityList[i]->MaxFrames;        
     //switched character becomes transition entity
      FieldEntity::EntityList[i]->X = transitionEntity.X;  
      FieldEntity::EntityList[i]->Y = transitionEntity.Y;
      FieldEntity::EntityList[i]->status = transitionEntity.status;
      FieldEntity::EntityList[i]->Width = transitionEntity.Width;
      FieldEntity::EntityList[i]->Height = transitionEntity.Height;
      FieldEntity::EntityList[i]->AnimState = transitionEntity.AnimState;
      FieldEntity::EntityList[i]->MaxFrames = transitionEntity.MaxFrames;

     newballEntity.AnimState=0;
     newballEntity.MaxFrames=1;
}

void Field::checkCatch()
{
  int i;
  float checkOffY = 0;
  float checkOffX = 0;
  int checkOffStatus = 0;
  //loops through all players
  for(i = 1;i < FieldEntity::EntityList.size()-1;i++) {
    checkOffY = FieldEntity::EntityList[i]->Y;
    checkOffX = FieldEntity::EntityList[i]->X;
    checkOffStatus = FieldEntity::EntityList[i]->status;
	 //only checks for offense
    if (checkOffStatus == 1 || checkOffStatus == 4 || checkOffStatus == 5 || checkOffStatus == 6){
		//checks if the ball lands close to a player
      if( fabs(checkOffY-newballEntity.Y)<38 && fabs(checkOffX-newballEntity.X)<38 )
      {
        canPass=0;
		  //gives the player the ball and exits function
        handOff();  
        return;
      }  
    }
  }
  //if it doesn't find a player where ball lands
  OnIncomplete();    
}

void Field::OnIncomplete()
{
	 //resets qb to last postioin
    qbEntity.Y=qbEntity.yInitial-100;
    qbEntity.X=qbEntity.xInitial;
	 //reset ball to old line of scrimage
    newballEntity.Y=qbEntity.yInitial-200;
    newballEntity.X=qbEntity.xInitial;

    float Ychange;
    //find out how far players are from original position
    Ychange=qbEntity.Y-qbEntity.resetY;
	 //reset all player status
    newballEntity.AnimState = 0;
    qbEntity.AnimState= 0;

    player1Entity.status = 1; player1Entity.AnimState = 0;
    player2Entity.status = 1; player2Entity.AnimState = 0;
    player3Entity.status = 1; player3Entity.AnimState = 0;
    player4Entity.status = 1; player4Entity.AnimState = 0;
    player5Entity.status = 2; player5Entity.AnimState = 1;
    player6Entity.status = 2; player6Entity.AnimState = 1;
    player7Entity.status = 2; player7Entity.AnimState = 1;
    player8Entity.status = 2; player8Entity.AnimState = 1;
    player9Entity.status = 4; player9Entity.AnimState = 0;
    player10Entity.status = 5; player10Entity.AnimState = 0;
    player11Entity.status = 7; player11Entity.AnimState = 1;
    player12Entity.status = 8; player12Entity.AnimState = 1;
    player13Entity.status = 9; player13Entity.AnimState = 1;
    player14Entity.status = 6; player14Entity.AnimState = 0;
     
	// >130 is the normal set
   if(newballEntity.Y > 130){
	   //set all players to old postions
      for(int i = 1; i< FieldEntity::EntityList.size()-1;i++){
        (FieldEntity::EntityList[i])->setOriginal(Ychange+100);
        if(FieldEntity::EntityList[i]->Y < 0){
          FieldEntity::EntityList[i]->Y = 30;
          }
      }
      newballEntity.Y = newballEntity.Y+100;
    }
	 // <130 is the goaline set
    if(newballEntity.Y < 130){
		   //reset player to goaline position
         for(int i = 1; i< FieldEntity::EntityList.size()-1; i++){
           int checkStatus =0;
           checkStatus = FieldEntity::EntityList[i]->status;
           if((checkStatus ==1 || checkStatus == 4 || checkStatus == 5 || checkStatus ==6)){
             FieldEntity::EntityList[i]->Y = newballEntity.Y;
             FieldEntity::EntityList[i]->X = FieldEntity::EntityList[i]->resetX;
           }
           if(checkStatus == 2 || checkStatus == 7 || checkStatus ==8 || checkStatus ==9){
             FieldEntity::EntityList[i]->Y = 90;
             FieldEntity::EntityList[i]->X = FieldEntity::EntityList[i]->resetX;
           }
         }
         qbEntity.Y = qbEntity.Y+100;
         newballEntity.Y = newballEntity.Y+100;
         canPass = 1;
    }  
	 //chekc for lose on 4th down
    if(downNumber>3) onLoser();
    downNumber++;
    live=0;
    canPass=1;
}

void Field::onTouchdown(){
	  //freeze game and set td to display
     live=0;
     tdgo=1;
}
void Field::onLoser(){
	  //freeze game and set loser to display
     live = 0;
     losergo=1;
}

void Field::afterTD(){
    float Ychange;
    //no change from original positions(want to reset to starting values)
    Ychange=0;
    //place ball at original postion
    newballEntity.Y=newballEntity.resetY;
    newballEntity.X=newballEntity.resetX;
		
	 //reset all states and status
    newballEntity.AnimState = 0;
    qbEntity.AnimState= 0;

    player1Entity.status = 1; player1Entity.AnimState = 0;
    player2Entity.status = 1; player2Entity.AnimState = 0;
    player3Entity.status = 1; player3Entity.AnimState = 0;
    player4Entity.status = 1; player4Entity.AnimState = 0;
    player5Entity.status = 2; player5Entity.AnimState = 1;
    player6Entity.status = 2; player6Entity.AnimState = 1;
    player7Entity.status = 2; player7Entity.AnimState = 1;
    player8Entity.status = 2; player8Entity.AnimState = 1;
    player9Entity.status = 4; player9Entity.AnimState = 0;
    player10Entity.status = 5; player10Entity.AnimState = 0;
    player11Entity.status = 7; player11Entity.AnimState = 1;
    player12Entity.status = 8; player12Entity.AnimState = 1;
    player13Entity.status = 9; player13Entity.AnimState = 1;
    player14Entity.status = 6; player14Entity.AnimState = 0;
    //place all players at their original postions(since Ychange=0)
    for(int i = 1;i < FieldEntity::EntityList.size()-1;i++) {
          (FieldEntity::EntityList[i])->setOriginal(Ychange);
    }
	 //first down
    downNumber=1;
    canPass=1;
}

void Field::hike(){
	  //move ball back to qb and allow game to start when it gets there
     if(newballEntity.Y < qbEntity.Y){
        moveBall(0,0.5,17);
     }
     else {
        live=1;
        hikeball=0;
     }
}
